﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class CycleCountClassDA : ICycleCountClassDA
    {
        private string CLASS_DESC = "@CLASS_DESC";
        private string CLASS_ID = "@CLASS_ID";
        private string CREATED_BY = "@CREATED_BY";
        private string CYCLE_DAYS = "@CYCLE_DAYS";
        private string IS_ACTIVE = "@IS_ACTIVE";
        private string NEG_CNT_VARIANCE = "@NEG_CNT_VARIANCE";
        private string NEG_VALUE_VARIANCE = "@NEG_VALUE_VARIANCE";
        private string POS_QTY_VARIANCE = "@POS_QTY_VARIANCE";
        private string POS_VALUE_VARIANCE = "@POS_VALUE_VARIANCE";
        private string SQL_CHECK_CYCLE_COUNT_CLASS_ID_UNIQUE = " SELECT COUNT(1) FROM @_@CYCLE_COUNT_CLASS WHERE CLASS_ID = @CLASS_ID ";
        private string SQL_DELETE_CYCLE_COUNT_CLASS = " DELETE FROM @_@CYCLE_COUNT_CLASS WHERE CLASS_ID = @CLASS_ID ";
        private string SQL_INSERT_CYCLE_COUNT_CLASS = " INSERT INTO @_@CYCLE_COUNT_CLASS ( WH_ID, CLASS_ID, CLASS_DESC, CYCLE_DAYS, POS_QTY_VARIANCE, NEG_CNT_VARIANCE, POS_VALUE_VARIANCE, NEG_VALUE_VARIANCE, IS_ACTIVE, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @CLASS_ID, @CLASS_DESC, @CYCLE_DAYS, @POS_QTY_VARIANCE, @NEG_CNT_VARIANCE, @POS_VALUE_VARIANCE, @NEG_VALUE_VARIANCE, @IS_ACTIVE, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_CYCLE_COUNT_CLASS = " SELECT  ROWID, WH_ID, CLASS_ID, CLASS_DESC, CYCLE_DAYS, POS_QTY_VARIANCE, NEG_CNT_VARIANCE, POS_VALUE_VARIANCE, NEG_VALUE_VARIANCE, IS_ACTIVE, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@CYCLE_COUNT_CLASS ";
        private string SQL_UPDATE_CYCLE_COUNT_CLASS = " UPDATE @_@CYCLE_COUNT_CLASS SET WH_ID = @WH_ID, CLASS_DESC = @CLASS_DESC, CYCLE_DAYS = @CYCLE_DAYS, POS_QTY_VARIANCE = @POS_QTY_VARIANCE, NEG_CNT_VARIANCE = @NEG_CNT_VARIANCE, POS_VALUE_VARIANCE = @POS_VALUE_VARIANCE, NEG_VALUE_VARIANCE = @NEG_VALUE_VARIANCE, IS_ACTIVE = @IS_ACTIVE, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE CLASS_ID = @CLASS_ID  ";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";

        public bool CheckCycleCountClassIDUnique(string cycleCountClassID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CLASS_ID", cycleCountClassID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_CYCLE_COUNT_CLASS_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckCycleCountClassIDUnique(DataBase dataBase, DbTransaction tran, string cycleCountClassID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CLASS_ID", cycleCountClassID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_CYCLE_COUNT_CLASS_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteCycleCountClass(List<string> cycleCountClassID, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < cycleCountClassID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CLASS_ID", cycleCountClassID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_CYCLE_COUNT_CLASS.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteCycleCountClass(DataBase dataBase, DbTransaction tran, List<string> cycleCountClassID, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < cycleCountClassID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CLASS_ID", cycleCountClassID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_CYCLE_COUNT_CLASS.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllCycleCountClass(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_CYCLE_COUNT_CLASS.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllCycleCountClass(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_CYCLE_COUNT_CLASS.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public CycleCountClassInfo GetCycleCountClassByID(string cyclecountclassID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_CYCLE_COUNT_CLASS.Replace("@_@", whLoginID) + " WHERE CLASS_ID = @CLASS_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CLASS_ID", cyclecountclassID) };
            CycleCountClassInfo cycleCountClassInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    cycleCountClassInfo = new CycleCountClassInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["CLASS_ID"].ToString(), reader["CLASS_DESC"].ToString(), (reader["CYCLE_DAYS"].ToString() != "") ? int.Parse(reader["CYCLE_DAYS"].ToString()) : 0, (reader["POS_QTY_VARIANCE"].ToString() != "") ? decimal.Parse(reader["POS_QTY_VARIANCE"].ToString()) : 0M, (reader["NEG_CNT_VARIANCE"].ToString() != "") ? decimal.Parse(reader["NEG_CNT_VARIANCE"].ToString()) : 0M, (reader["POS_VALUE_VARIANCE"].ToString() != "") ? decimal.Parse(reader["POS_VALUE_VARIANCE"].ToString()) : 0M, (reader["NEG_VALUE_VARIANCE"].ToString() != "") ? decimal.Parse(reader["NEG_VALUE_VARIANCE"].ToString()) : 0M, reader["IS_ACTIVE"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return cycleCountClassInfo;
        }

        public CycleCountClassInfo GetCycleCountClassByID(DataBase dataBase, DbTransaction tran, string cyclecountclassID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_CYCLE_COUNT_CLASS.Replace("@_@", whLoginID) + " WHERE CLASS_ID = @CLASS_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CLASS_ID", cyclecountclassID) };
            CycleCountClassInfo cycleCountClassInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                cycleCountClassInfo = new CycleCountClassInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["CLASS_ID"].ToString(), reader["CLASS_DESC"].ToString(), (reader["CYCLE_DAYS"].ToString() != "") ? int.Parse(reader["CYCLE_DAYS"].ToString()) : 0, (reader["POS_QTY_VARIANCE"].ToString() != "") ? decimal.Parse(reader["POS_QTY_VARIANCE"].ToString()) : 0M, (reader["NEG_CNT_VARIANCE"].ToString() != "") ? decimal.Parse(reader["NEG_CNT_VARIANCE"].ToString()) : 0M, (reader["POS_VALUE_VARIANCE"].ToString() != "") ? decimal.Parse(reader["POS_VALUE_VARIANCE"].ToString()) : 0M, (reader["NEG_VALUE_VARIANCE"].ToString() != "") ? decimal.Parse(reader["NEG_VALUE_VARIANCE"].ToString()) : 0M, reader["IS_ACTIVE"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return cycleCountClassInfo;
        }

        public DataSet GetCycleCountClassByQueryList(List<string> sqlWhere, CycleCountClassQueryEntity cyclecountclassQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_CYCLE_COUNT_CLASS.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!cyclecountclassQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, cyclecountclassQuery.CurrentPage, cyclecountclassQuery.PageSize, cyclecountclassQuery.SortField, cyclecountclassQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetCycleCountClassByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, CycleCountClassQueryEntity cyclecountclassQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_CYCLE_COUNT_CLASS.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!cyclecountclassQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, cyclecountclassQuery.CurrentPage, cyclecountclassQuery.PageSize, cyclecountclassQuery.SortField, cyclecountclassQuery.SortDirection);
            }
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertCycleCountClass(CycleCountClassInfo cycleCountClassInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    cycleCountClassInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_CycleCountClass_Parameters(cycleCountClassInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_CYCLE_COUNT_CLASS.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertCycleCountClass(DataBase dataBase, DbTransaction tran, CycleCountClassInfo cycleCountClassInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    cycleCountClassInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_CycleCountClass_Parameters(cycleCountClassInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_CYCLE_COUNT_CLASS.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_CycleCountClass_Parameters(CycleCountClassInfo cycleCountClassInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.CLASS_ID, SqlDbType.VarChar, 1), new SqlParameter(this.CLASS_DESC, SqlDbType.VarChar, 50), new SqlParameter(this.CYCLE_DAYS, SqlDbType.Int, 4), new SqlParameter(this.POS_QTY_VARIANCE, SqlDbType.Decimal, 13), new SqlParameter(this.NEG_CNT_VARIANCE, SqlDbType.Decimal, 13), new SqlParameter(this.POS_VALUE_VARIANCE, SqlDbType.Decimal, 13), new SqlParameter(this.NEG_VALUE_VARIANCE, SqlDbType.Decimal, 13), new SqlParameter(this.IS_ACTIVE, SqlDbType.VarChar, 1), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20) };
            if (!string.IsNullOrEmpty(cycleCountClassInfo.WhID))
            {
                paramArray[0].Value = cycleCountClassInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = cycleCountClassInfo.ClassID;
            if (!string.IsNullOrEmpty(cycleCountClassInfo.ClassDesc))
            {
                paramArray[2].Value = cycleCountClassInfo.ClassDesc;
            }
            else
            {
                paramArray[2].Value = DBNull.Value;
            }
            paramArray[3].Value = cycleCountClassInfo.CycleDays;
            paramArray[4].Value = cycleCountClassInfo.PosQtyVariance;
            paramArray[5].Value = cycleCountClassInfo.NegCntVariance;
            paramArray[6].Value = cycleCountClassInfo.PosValueVariance;
            paramArray[7].Value = cycleCountClassInfo.NegValueVariance;
            paramArray[8].Value = cycleCountClassInfo.IsActive;
            paramArray[9].Value = cycleCountClassInfo.CreatedBy;
            paramArray[10].Value = cycleCountClassInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateCycleCountClass(CycleCountClassInfo cycleCountClassInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    cycleCountClassInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_CycleCountClass_Parameters(cycleCountClassInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_CYCLE_COUNT_CLASS.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateCycleCountClass(DataBase dataBase, DbTransaction tran, CycleCountClassInfo cycleCountClassInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    cycleCountClassInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_CycleCountClass_Parameters(cycleCountClassInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_CYCLE_COUNT_CLASS.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

